package com.spark.tags;

import java.util.Iterator;
import java.util.List;

import com.spark.core.CacheManager;
import com.spark.core.SparkBuilder;
import com.spark.core.SparkContext;
import com.spark.core.SparkTemplate;
import com.spark.core.XMap;

public class Cache extends SparkTag {

	@Override
	public String execute(XMap view, SparkContext context) throws Exception {
		SparkTemplate template = context.getTemplate();
		String id = view.getString("id");
		String str = null;
		if(id!=null){
			str = CacheManager.getInstance().getTagHTML(template.getFilePath(), id);
		}
		if(str==null){
			StringBuilder sb = new StringBuilder();
			List<XMap> children = view.getChilds();
			if (children != null) {
				Iterator<XMap> it = children.iterator();
				while (it.hasNext()) {
					XMap child = it.next();
					sb.append(SparkBuilder.build(child, context));
				}
			}
			str = sb.toString();
			CacheManager.getInstance().setTagHTML(template.getFilePath(), id, str);
		}
		
		return str;
	}

}
